import $req from './request';
import { encode, decode } from 'js-base64';
export interface smsLoginType {
    phone: string;
    companyId: string;
    verifyCode: string;
    clientType?: string;
}

export interface accountLoginType {
    account: string;
    companyId: string;
    password?: string;
    clientType?: string;
}

export interface updatePasswordType {
    code: string;
    companyId: number;
    newPassword: string;
    phoneNumbers: string;
}

// 手机号获取已绑定的公司
export const getBindCompanyList = (params: anyObj) =>
    $req({
        url: '/company/open/getCompanyListByPhone',
        params,
        headers: {
            Authorization: '',
        },
    });

// 手机验证码登录
export const phoneLogin = (data: smsLoginType) =>
    $req({
        url: '/smsLogin',
        method: 'POST',
        headers: {
            Authorization: '',
        },
        data: {
            clientType: 'APPLET',
            ...data,
        },
    });

// 获取图形验证码
export const getCaptcha = (time: string | number) =>
    $req({
        url: `/getCaptcha/${time}`,
        headers: {
            Authorization: '',
        },
    });

// 发送短信验证码
export const sendCode = (data: anyObj) =>
    $req({
        url: `/sms/sendLoginMessage`,
        method: 'POST',
        data,
        headers: {
            Authorization: '',
        },
    });

// 微信获取手机号登录
export const getWxPhoneNumber = async (params: anyObj) =>
    await $req({
        url: '/wx/user/phone',
        headers: {
            Authorization: '',
        },
        params,
    });

// 密码登录
export const psdLogin = (data: accountLoginType) =>
    $req({
        url: '/login',
        method: 'POST',
        headers: {
            Authorization: '',
        },
        data: {
            clientType: 'APPLET',
            ...data,
            password: encode(data.password || ''),
        },
    });

// 退出登录
export const logout = () =>
    $req({
        url: '/logout',
    });

// 手机验证码修改密码
export const updatePwdByPhone = (data: anyObj) =>
    $req({
        url: '/sysUser/phoneUpdatePwd',
        method: 'POST',
        headers: {
            Authorization: '',
        },
        data: {
            clientType: 'APPLET',
            ...data,
            newPassword: encode(data.newPassword || ''),
        },
    });
